//
// Created by yangchao on 2022/5/25.
// 102. 二叉树的层序遍历: https://leetcode.cn/problems/binary-tree-level-order-traversal/
//
#include <vector>
#include <queue>
#include "../DataStructure/TreeNode.h"

using namespace std;

class LevelOrder {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> res;
        queue<TreeNode*> qu;
        if (root != nullptr) qu.push(root);
        while (!qu.empty()) {
            int len = qu.size();
            vector<int> level;
            for (int i = 0; i < len; ++i) {
                TreeNode* node = qu.front();
                qu.pop();
                level.push_back(node->val);
                if (node->left != nullptr) qu.push(node->left);
                if (node->right != nullptr) qu.push(node->right);
            }
            res.push_back(level);
        }
      return res;
    }
};